package view.frontend.order;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import model.CustomerSpecified;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class OrderCustomerListPanel extends JPanel
{
    private Order order;
    
    public OrderCustomerListPanel(final Order view, final CustomerSpecified customer)
    {
        order = view;
        
        setLayout(null);
        setSize(new Dimension(220, 130));
        setBorder(DesignLayout.T_FIELD_RED);
        
        JLabel lblPhone = new JLabel(customer.getCustomerTelephone().getNumber());
        lblPhone.setBounds(10, 5, 220, 18);
        lblPhone.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        JLabel lblName = new JLabel("<html>"+customer.getCustomerName().getName()+" (<b>ID:</b> "+customer.getCustomerName().getId()+")</html>");
        lblName.setBounds(10, 25, 220, 18);
        lblName.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        JLabel lblStreet = new JLabel("");
        lblStreet.setBounds(10, 45, 220, 18);
        lblStreet.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        JLabel lblZipCity = new JLabel("");
        lblZipCity.setBounds(10, 65, 220, 18);
        lblZipCity.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        if (customer.getCustomerAddress() != null)
        {
            lblStreet.setText(customer.getCustomerAddress().getStreet());
            lblZipCity.setText(customer.getCustomerAddress().getZipCode().getZipCode()+" "+customer.getCustomerAddress().getZipCode().getCity());
        }
        
        JButton btnAddToOrder = new JButton("ordre");
        btnAddToOrder.setFont(DesignLayout.STANDARD.deriveFont(11f));
        btnAddToOrder.setBorder(DesignLayout.T_FIELD_RED);
        btnAddToOrder.setBackground(Color.lightGray);
        btnAddToOrder.setPreferredSize(new Dimension(40, 30));
        btnAddToOrder.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                order.getOrderObject().setCustomerSpecified(0, customer);
            }
        });
        
        JButton btnAddToInvoice = new JButton("faktura");
        btnAddToInvoice.setFont(DesignLayout.STANDARD.deriveFont(11f));
        btnAddToInvoice.setBorder(DesignLayout.T_FIELD_RED);
        btnAddToInvoice.setBackground(Color.lightGray);
        btnAddToInvoice.setPreferredSize(new Dimension(45, 30));
        btnAddToInvoice.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                order.getOrderObject().setCustomerSpecified(1, customer);
            }
        });
        
        JButton btnAddToMortician = new JButton("bedemand");
        btnAddToMortician.setFont(DesignLayout.STANDARD.deriveFont(11f));
        btnAddToMortician.setBorder(DesignLayout.T_FIELD_RED);
        btnAddToMortician.setBackground(Color.lightGray);
        btnAddToMortician.setPreferredSize(new Dimension(65, 30));
        if (order.getOrderObject().getTombStone() == null)
        {
            btnAddToMortician.setEnabled(false);
        }
        else
        {
            if (customer.getCustomerName().isMortician() == false)
            {
                btnAddToMortician.setEnabled(false);
            }
            else
            {
                btnAddToMortician.setEnabled(true);
            }
        }
        
        btnAddToMortician.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                order.getOrderObject().getTombStone().setMortician(customer);
            }
        });
        
        JButton btnAddToAll = new JButton("alle");
        btnAddToAll.setFont(DesignLayout.STANDARD.deriveFont(11f));
        btnAddToAll.setBorder(DesignLayout.T_FIELD_RED);
        btnAddToAll.setBackground(Color.lightGray);
        btnAddToAll.setPreferredSize(new Dimension(40, 30));
        btnAddToAll.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (order.getOrderObject().getTombStone() == null)
                {
                    order.getOrderObject().setCustomerSpecified(0, customer);
                    order.getOrderObject().setCustomerSpecified(1, customer);
                }
                else
                {
                    order.getOrderObject().setCustomerSpecified(0, customer);
                    order.getOrderObject().setCustomerSpecified(1, customer);
                    if (customer.getCustomerName().isMortician() == true)
                    {
                        order.getOrderObject().getTombStone().setMortician(customer);
                    }
                }                
            }
        });
        
        add(lblPhone);
        add(lblName);
        add(lblStreet);
        add(lblZipCity);
        
        JPanel panelButtons = new JPanel();
        panelButtons.setBounds(4, 90, 210, 35);
        panelButtons.add(btnAddToOrder);
        panelButtons.add(btnAddToInvoice);
        panelButtons.add(btnAddToMortician);
        panelButtons.add(btnAddToAll);
        
        add(panelButtons);
    }
}